import sys
import requests
import util
from bs4 import BeautifulSoup


# region win4000 爬虫
class ImgInfo:
    url = ""
    dir = ""
    def __init__(self, url, dir_name):
        self.url = url
        self.dir = dir_name


def win4000_main():
    urls = [
        # "http://www.win4000.com/sjzt/xingganmeinv.html",
        # "http://www.win4000.com/sjzt/xingganmeinv_2.html",
        # "http://www.win4000.com/sjzt/xingganmeinv_3.html",
        # "http://www.win4000.com/sjzt/xingganmeinv_4.html",

        "http://www.win4000.com/sjzt/dilireba.html",
        "http://www.win4000.com/sjzt/dilireba_2.html",
        "http://www.win4000.com/sjzt/dilireba_3.html",
        "http://www.win4000.com/sjzt/dilireba_4.html",
    ]
    for url in urls:
        win4000(url)


def win4000(url):
    r = requests.get(url)
    r.encoding = "utf-8"
    soup = BeautifulSoup(r.text, "html.parser")
    uls = soup.find_all("ul")
    imgs = []
    for ul in uls:
        ul_class = ul.get("class")
        if ul_class and str(ul_class[0]) == "clearfix":
            for li in ul.contents:
                if str(li) != "\n":
                    for a in li.contents:
                        if a.find('img'):
                            if str(a) != "\n":
                                # print(a)
                                if a.get("alt"):
                                    # print(a.get("href"))
                                    # print(a.get("alt"))
                                    img = ImgInfo(a.get("href"), a.get("alt"))
                                    imgs.append(img)
                                # print(tag.text)
                                # print(a.get("href"))
    util.mkdir("img/win4000")
    for img in imgs:
        dir_path = "win4000/" + img.dir
        util.mkdir("img/" + dir_path)
        win4000_detail(dir_path, img.url)
    print(url + "\t下载完毕！！！")


pic_title_tag = "ptitle"
pic_next_tag = "pic-next-img"
pic_target_tag = "pic-meinv"


def win4000_down(dir_path, index, length, down_url):
    array = str(down_url).split("/")
    save = array[len(array) - 1]
    print("download %s 第 %s 张共 %s 张 ！！！" % (dir_path, str(index), str(length)))
    # print("download " + dir_path + " 第" + str(index) + "张共" + str(length) + "张!!!!")
    util.download_img(down_url, dir_path + "/" + save)


def win4000_detail(dir_path, url):
    # url = "http://www.win4000.com/mobile_detail_159241.html"
    headers = {
        "User-Agent":
            "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/74.0.3729.169 Safari/537.36"
    }
    r = requests.get(url, headers=headers)
    r.encoding = "utf-8"
    soup = BeautifulSoup(r.text, "html.parser")
    divs = soup.find_all("div")

    index = 0
    length = 0
    next_url = ""
    down_url = ""
    for div in divs:
        # 获取标题
        if div.get("class") and div.get("class")[0] == pic_title_tag:
            index = int(div.find("span").text)
            length = int(div.find("em").text)
        elif div.get("class") and div.get("class")[0] == pic_next_tag:
            next_url = div.find("a").get("href")
        elif div.get("class") and div.get("class")[0] == pic_target_tag:
            down_url = div.find("img").get("src")

    if index >= length:
        win4000_down(dir_path, index, length, down_url)
        print(dir_path + "下载完毕")
        return
    win4000_down(dir_path, index, length, down_url)
    win4000_detail(dir_path, next_url)

# endregion


if __name__ == '__main__':
    win4000_main()
    print(sys.version)